﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework;
using CNVP.JShop.UI;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Member
{
    public partial class OrderAdd : UserPage
    {
        public string Province = "0", City = "0", Area = "0";
        public int GetAddress_Int = 0;
        public string ProductIDS = "";
        public string ProDetailIDS = "";
        public string AmountS = "";
        decimal DisCountPercent = 0;
        BLL.CNVP_Products bll_Products = new BLL.CNVP_Products();
        BLL.CNVP_UserLevel bll_UserLevel = new BLL.CNVP_UserLevel();
        BLL.CNVP_ProductsDetail bll_ProductsDetail = new BLL.CNVP_ProductsDetail();
        protected void Page_Load(object sender, EventArgs e)
        {
            ProductIDS = string.IsNullOrEmpty(Request.QueryString["ProductIDS"]) ? "0" : Request.QueryString["ProductIDS"];//主产品IDS
            ProDetailIDS = string.IsNullOrEmpty(Request.QueryString["ProDetailIDS"]) ? "0" : Request.QueryString["ProDetailIDS"];//详细产品IDS
            AmountS = string.IsNullOrEmpty(Request.QueryString["SaleNumArray"]) ? "1" : Request.QueryString["SaleNumArray"];//销售数量
            if (!IsPostBack)
            {
                if (ProductIDS != "0")
                {
                    StockInfo();
                    if (this.CookieString != null)
                    {
                        var model_UserLevel = new BLL.CNVP_UserLevel().GetSingle_CNVP_UserLevelById(CookieUserLevelID);
                        if (model_UserLevel != null)
                        {
                            DisCountPercent = model_UserLevel.DisCountPercent;
                        }
                    }
                    DataInfo();
                    RefShopMustInfo();
                    OrderProList();
                    OrderTotal();
                }
                else
                {
                    BasePage.MessageBoxGoHistory("请不要随意改变参数！");
                }
            }
        }

        /// <summary>
        /// 必须先判断库存
        /// </summary>
        private void StockInfo()
        {
            if (!string.IsNullOrEmpty(ProductIDS) && !string.IsNullOrEmpty(ProDetailIDS) && !string.IsNullOrEmpty(AmountS))
            {
                string[] PIDSArray = ProductIDS.Split(Convert.ToChar(","));//主表ID
                for (int i = 0; i < PIDSArray.Length; i++)
                {
                    int _PIDS = Convert.ToInt32(PIDSArray[i]);
                    var model_Products = bll_Products.GetSingle_CNVP_ProductsById(_PIDS);
                    string[] SaleNumArray = AmountS.Split(Convert.ToChar(","));//销售数量
                    int SaleNum_Int = Convert.ToInt32(SaleNumArray[i]);
                    string[] IDSArray = ProDetailIDS.Split(Convert.ToChar(","));//详细产品IDS
                    int proDetailID_Int = Convert.ToInt32(IDSArray[i]);
                    if (proDetailID_Int != 0)
                    {
                        var model_ProductsDetail = bll_ProductsDetail.GetSingle_CNVP_ProductsDetailById(proDetailID_Int);
                        if (model_ProductsDetail != null)
                        {
                            if (SaleNum_Int > model_ProductsDetail.StockNum)
                            {
                                BasePage.MessageBoxGoHistory("" + model_Products.ProductName + "--" + model_ProductsDetail.ProDetailNo + "  库存量不足！");
                                return;
                            }
                        }
                    }
                    else
                    {
                        
                        if (model_Products != null)
                        {
                            if (SaleNum_Int > model_Products.StockTotalNum)
                            {
                                BasePage.MessageBoxGoHistory("" + model_Products.ProductName + "  库存量不足！");
                                return;
                            }
                        }
                    }

                }
            }
        }

        private void DataInfo()
        {
            #region 收货确认信息
            var GetAddressList = new BLL.CNVP_GetAddress().GetAll_CNVP_GetAddressByUsersID(CookieUserID);
            if (GetAddressList != null && GetAddressList.Count > 0)
            {
                GetAddress_Int = GetAddressList.Count;
                rpList.DataSource = GetAddressList;
                rpList.DataBind();
            }
            #endregion
            #region 支付方式
            var ColumnPayWay = new BLL.CNVP_Column().GetAll_CNVP_Column("ColumnType in(5,7)");
            string  html = "";
            html += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"  widtd=\"100%\">";
            html += "	 <tbody >";
            for (int i = 0; i < ColumnPayWay.Count; i++)
            {
                string temp = "";
                if (i == 0)
                {
                    temp = "checked=\"checked\"";
                }
                html += "		<tr >";
                html += "			<th style=\"width:150px; text-align:left;\">";
                html += "				<input id=\"PayWay" + ColumnPayWay[i].ID + "\" type=\"radio\" value=\"" + ColumnPayWay[i].ID + "\" name=\"PayWay\" " + temp + " />" + ColumnPayWay[i].ColumnName + "";
                html += "			</th>";
                html += "			<td>";
                html += "				" + ColumnPayWay[i].ColumnName + "";
                html += "			</td>";
                html += "		</tr>";
            }
            html += "	</tbody>";
            html += "</table>";
            lbPayWay.Text = html;
            #endregion

        }

        /// <summary>
        /// 购物必填信息
        /// </summary>
        private void RefShopMustInfo()
        {
            if (!string.IsNullOrEmpty(ProductIDS) && !string.IsNullOrEmpty(ProDetailIDS) && !string.IsNullOrEmpty(AmountS))
            {
                string _ProDetailNo = "";
                decimal _SalePrice = 0;
                string AddTr = "";
                string[] PIDSArray = ProductIDS.Split(Convert.ToChar(","));
                for (int i = 0; i < PIDSArray.Length; i++)
                {
                    int _PIDS = Convert.ToInt32(PIDSArray[i]);
                    string _StandardValueName = "";
                    string[] SaleNumArray = AmountS.Split(Convert.ToChar(","));//销售数量
                    var Product_model = bll_Products.GetSingle_CNVP_ProductsById(_PIDS);
                    //详细产品IDS
                    int _IDS = 0;
                    if (!string.IsNullOrEmpty(ProDetailIDS))
                    {
                        string[] IDSArray = ProDetailIDS.Split(Convert.ToChar(","));//详细产品IDS
                        _IDS = Convert.ToInt32(IDSArray[i]);
                        if (_IDS != 0)
                        {
                            var ProductDetail_model = bll_ProductsDetail.GetSingle_CNVP_ProductsDetailById(_IDS);
                            if (ProductDetail_model != null)
                            {
                                _ProDetailNo = ProductDetail_model.ProDetailNo;

                                string StandardValueName = "";
                                string _StandardValueIDs=ProductDetail_model.StandardValueIDs;
                                if (!string.IsNullOrEmpty(_StandardValueIDs))
                                {
                                    StandardValueName = "（";
                                    string[] StandardValueIDsArray = _StandardValueIDs.Split(Convert.ToChar(","));
                                    for (int k = 0; k < StandardValueIDsArray.Length; k++)
                                    {
                                        var model_StandardValue = new BLL.CNVP_StandardValue().GetSingle_CNVP_StandardValueById(int.Parse(StandardValueIDsArray[k]));
                                        if (model_StandardValue != null)
                                        {
                                            StandardValueName += model_StandardValue.StandardValueName + "、";
                                        }
                                    }

                                    if (!string.IsNullOrEmpty(StandardValueName))
                                    {
                                        StandardValueName = StandardValueName.Substring(0, StandardValueName.Length - 1) + "）";
                                    }
                                }
                                _StandardValueName = StandardValueName;

                                _SalePrice = ProductDetail_model.SalePrice;
                            }
                        }
                        else
                        {
                            if (Product_model != null)
                            {

                                _ProDetailNo = Product_model.ProductNo;
                                _SalePrice = Product_model.TradePrice;
                            }
                        }
                    }
                    else
                    {
                        if (Product_model != null)
                        {
                            _ProDetailNo = Product_model.ProductNo;
                            _SalePrice = Product_model.TradePrice;
                        }
                    }
                    if (Product_model != null)
                    {
                        var model_ProductType = new BLL.CNVP_ProductType().GetSingle_CNVP_ProductTypeById(Product_model.ProductTypeID);
                        if (model_ProductType != null)
                        {
                            byte IsRefShopMustInfo = model_ProductType.IsRefShopMustInfo;
                            string _ValueName = _StandardValueName + "<font color=\"red\" id=\"FontSaleNum" + _PIDS + "_" + _IDS + "\"> x " + SaleNumArray[i] + "</font>";
                            ViewState["LengthName" + i + ""] = Product_model.ProductName + "-" + _ProDetailNo + "-" + _StandardValueName;
                            if (IsRefShopMustInfo == 1)
                            {
                                AddTr += "<h3>填写购买 " + Product_model.ProductName + "-" + _ProDetailNo + "-" + _ValueName + " 时所需信息";
                                AddTr += "<input type=\"hidden\" id=\"ProID_ProDetailIDS" + _PIDS + "_" + _IDS + "\" name=\"ProID_ProDetailIDS\" value=\"" + _PIDS + "_" + _IDS + "\" /></h3>";
                                var ProTypeRefShopMustInfo_List = new BLL.CNVP_ProTypeRefShopMustInfo().GetAll_CNVP_ProTypeRefShopMustInfo(Convert.ToInt16(Product_model.ProductTypeID));
                                if (ProTypeRefShopMustInfo_List != null && ProTypeRefShopMustInfo_List.Count > 0)
                                {
                                    for (int j = 0; j < ProTypeRefShopMustInfo_List.Count; j++)
                                    {
                                        AddTr += "<table id=\"tableEdit\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
                                        string html = "";
                                        if (ProTypeRefShopMustInfo_List[j].ShowWay == 0)
                                        {
                                            html += "<tr>";
                                            html += "   <th>";
                                            html += "   " + ProTypeRefShopMustInfo_List[j].MustInfoName + "";
                                            html += "       <input id=\"CKRefShopMustInfo" + _PIDS + "_" + _IDS + "\" name=\"CKRefShopMustInfo" + _PIDS + "_" + _IDS + "\" type=\"checkbox\" checked=\"checked\" value=\"" + ProTypeRefShopMustInfo_List[j].ID + "\" class=\"Display\" />";
                                            html += "   </th>";
                                            html += "   <td>";
                                            html += "   <input type=\"text\" id=\"MustInfoValue" + _PIDS + "_" + _IDS + "\" name=\"MustInfoValue" + _PIDS + "_" + _IDS + "\" class=\"NoBackgroundImage\" /></td>";
                                            html += "</tr>";
                                        }
                                        else if (ProTypeRefShopMustInfo_List[j].ShowWay == 1)
                                        {
                                            html += "<tr>";
                                            html += "   <th>";
                                            html += "   " + ProTypeRefShopMustInfo_List[j].MustInfoName + "";
                                            html += "       <input id=\"CKRefShopMustInfo" + _PIDS + "_" + _IDS + "\" name=\"CKRefShopMustInfo" + _PIDS + "_" + _IDS + "\" type=\"checkbox\" checked=\"checked\" value=\"" + ProTypeRefShopMustInfo_List[j].ID + "\" class=\"Display\" />";
                                            html += "   </th>";
                                            html += "   <td>";
                                            html += "   <textarea id=\"MustInfoValue" + _PIDS + "_" + _IDS + "\" cols=\"20\"  name=\"MustInfoValue" + _PIDS + "_" + _IDS + "\" rows=\"5\" class=\"NoBackgroundImage\"></textarea></td>";
                                            html += "</tr>";
                                        }
                                        else if (ProTypeRefShopMustInfo_List[j].ShowWay == 2)
                                        {
                                            html += "<tr>";
                                            html += "   <th>";
                                            html += "   " + ProTypeRefShopMustInfo_List[j].MustInfoName + "";
                                            html += "       <input id=\"CKRefShopMustInfo" + _PIDS + "_" + _IDS + "\" name=\"CKRefShopMustInfo" + _PIDS + "_" + _IDS + "\" type=\"checkbox\" checked=\"checked\" value=\"" + ProTypeRefShopMustInfo_List[j].ID + "\" class=\"Display\" />";
                                            html += "   </th>";
                                            html += "   <td>";
                                            html += "   <select id=\"MustInfoValue" + _PIDS + "_" + _IDS + "\" name=\"MustInfoValue" + _PIDS + "_" + _IDS + "\" >";
                                            string _MustInfoValue = ProTypeRefShopMustInfo_List[j].MustInfoValue;
                                            if (!string.IsNullOrEmpty(_MustInfoValue))
                                            {
                                                string[] MustInfoValueArray = _MustInfoValue.Split(Convert.ToChar("|"));
                                                for (int k = 0; k < MustInfoValueArray.Length; k++)
                                                {
                                                    html += "       <option value=\"" + MustInfoValueArray[k] + "\">" + MustInfoValueArray[k] + "</option>";
                                                }
                                            }
                                            html += "   </select>";
                                            html += "   </td>";
                                            html += "</tr>";
                                        }
                                        AddTr += html;
                                        AddTr += "</table>";

                                    }
                                }
                            }
                            ShopMustInfo.InnerHtml = AddTr;
                        }
                    }
                }
            }
        }

        private void OrderProList()
        {
            if (!string.IsNullOrEmpty(ProductIDS) && !string.IsNullOrEmpty(ProDetailIDS) && !string.IsNullOrEmpty(AmountS))
            {
                string _ProDetailNo = "";
                decimal _SalePrice = 0;

                string html = "";
                html += "                    <table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" class=\"tableEdit\">";
                html += "                        <tr>";
                html += "                            <th nowrap=\"nowrap\" style=\"width:40px;\">";
                html += "                                序号</th>";
                html += "                            <th nowrap=\"nowrap\">";
                html += "                                货号</th>";
                html += "                            <th nowrap=\"nowrap\" style=\"width:340px;\">";
                html += "                                商品名称</th>";
                html += "                            <th nowrap=\"nowrap\" >";
                html += "                                单价</th>";
                html += "                            <th nowrap=\"nowrap\">";
                html += "                                数量</th>";
                html += "                            <th nowrap=\"nowrap\">";
                html += "                                小计</th>";
                html += "                        </tr>";
                string AddTr = "";
                string[] PIDSArray = ProductIDS.Split(Convert.ToChar(","));//主表ID
                for (int i = 0; i < PIDSArray.Length; i++)
                {
                    int _PIDS = Convert.ToInt32(PIDSArray[i]);
                    string[] SaleNumArray = AmountS.Split(Convert.ToChar(","));//销售数量
                    var model_Products = bll_Products.GetSingle_CNVP_ProductsById(_PIDS);
                    int _IDS=0;
                    if (!string.IsNullOrEmpty(ProDetailIDS))//详细产品IDS
                    {
                        string[] IDSArray = ProDetailIDS.Split(Convert.ToChar(","));//详细产品IDS
                        _IDS=Convert.ToInt32(IDSArray[i]);
                        if (_IDS != 0)
                        {
                            var model = bll_ProductsDetail.GetSingle_CNVP_ProductsDetailById(_IDS);
                            if (model != null)
                            {
                                _ProDetailNo = model.ProDetailNo;
                                _SalePrice = model.SalePrice;
                            }
                        }
                        else
                        {
                            if (model_Products != null)
                            {
                                _ProDetailNo = model_Products.ProductNo;
                                _SalePrice = model_Products.TradePrice;
                            }
                        }

                    }
                    else
                    {
                        if (model_Products != null)
                        {
                            _ProDetailNo = model_Products.ProductNo;
                            _SalePrice = model_Products.TradePrice;
                        }
                    }
                    AddTr += "                        <tr>";
                    AddTr += "                            <td>";
                    AddTr += "                                " + (i + 1) + "</td>";
                    AddTr += "                            <td>";
                    AddTr += "                                " + _ProDetailNo + "</td>";
                    AddTr += "                            <td>";
                    if (model_Products != null)
                    {
                        string _ImgProLoad = model_Products.ImgProLoad;//图片
                        int _ProductClassID = model_Products.ProductClassID;
                        AddTr += "                  <input type=\"hidden\" id=\"ImgProLoad" + _PIDS + "_" + _IDS + "\" name=\"ImgProLoad\" value=\"" + _ImgProLoad + "\"  />";
                        AddTr += "                  <input type=\"hidden\" id=\"ProductClassID" + _PIDS + "_" + _IDS + "\" name=\"ProductClassID\" value=\"" + _ProductClassID + "\"  />";
                        AddTr += "                  <input type=\"hidden\" id=\"LengthName" + _PIDS + "_" + _IDS + "\" name=\"LengthName\" value=\"" + ViewState["LengthName" + i + ""] + "\"  />";
                        AddTr += "                  <input type=\"hidden\" id=\"ProDetailNo" + _PIDS + "_" + _IDS + "\" name=\"ProDetailNo\" value=\"" + _ProDetailNo + "\"  />";

                        AddTr += "                  <a href=\"/ProductDetail_" + _ProductClassID + "_" + _PIDS + ".aspx\" target=\"_blank\" title=\"" + ViewState["LengthName" + i + ""] + "\">";
                        AddTr += "                                <img align=\"absMiddle\"  height=\"60px\" width=\"60px\" class=\"BuyIsPrintPic\" ";
                        AddTr += "                                    src=\"../UploadFile/Product/" + _ImgProLoad + "\" />";
                        AddTr += "                  </a>";
                        AddTr += "                  <a href=\"/ProductDetail_" + _ProductClassID + "_" + _PIDS + ".aspx\" target=\"_blank\" title=\"" + ViewState["LengthName" + i + ""] + "\">";
                        AddTr += "                                " + ViewState["LengthName" + i + ""] + "";
                        AddTr += "                  </a>";
                    }
                    AddTr += "                            </td>";
                    AddTr += "                            <td>";
                    Decimal decMoney = 0;
                    if (this.CookieString != null)
                    {
                        decMoney = _SalePrice * DisCountPercent;
                    }
                    else
                    {
                        decMoney = _SalePrice;
                    }

                    AddTr += "                              <input type=\"hidden\" id=\"SalePrice" + _PIDS + "_" + _IDS + "\" name=\"SalePrice\" value=\"" + decMoney.ToString("f2") + "\"  />";
                    
                    AddTr += "                                ￥";
                    AddTr += "                                  <span id=\"Price" + _PIDS + "_" + _IDS + "\">" + decMoney.ToString("f2") + "</span>";
                    AddTr += "                                元</td>";
                    AddTr += "                            <td>";
                    AddTr += "                                <input type=\"text\" id=\"Num" + _PIDS + "_" + _IDS + "\" name=\"Num\" class=\"saleNum\" value=\"" + SaleNumArray[i] + "\" onchange=\"ProductsOnchang(" + _PIDS + "," + _IDS + ")\"  />";
                    AddTr += "                                <input type=\"hidden\" id=\"NumCompare" + _PIDS + "_" + _IDS + "\" name=\"NumCompare\" value=\"" + SaleNumArray[i] + "\"> ";
                    AddTr += "                            </td>";
                    decimal TotalPrice = decMoney * int.Parse(SaleNumArray[i]);
                    AddTr += "                              <td>";
                    AddTr += "                                ￥";
                    AddTr += "                                  <span id=\"spantotalPrice" + _PIDS + "_" + _IDS + "\">" + TotalPrice.ToString("f2") + "</span>";
                    AddTr += "                                  <input type=\"hidden\" id=\"totalPrice" + _PIDS + "_" + _IDS + "\" name=\"totalPrice\" value=\"" + TotalPrice.ToString("f2") + "\" />";
                    AddTr += "                               元</td>";
                    AddTr += "                        </tr>";
                }
                html += AddTr;
                html += "                    </table>";
                lbProductList.Text = html;
            }
        }

        private void OrderTotal()
        {
            if (!string.IsNullOrEmpty(ProductIDS) && !string.IsNullOrEmpty(ProDetailIDS) && !string.IsNullOrEmpty(AmountS))
            {
                decimal _SalePrice = 0;

                string[] PIDSArray = ProductIDS.Split(Convert.ToChar(","));
                decimal SalePriceTatol = 0;
                for (int i = 0; i < PIDSArray.Length; i++)
                {
                    int _PIDS = Convert.ToInt32(PIDSArray[i]);
                    string[] SaleNumArray = AmountS.Split(Convert.ToChar(","));//销售数量
                    var model_Products = bll_Products.GetSingle_CNVP_ProductsById(_PIDS);
                    if (!string.IsNullOrEmpty(ProDetailIDS))//详细产品IDS
                    {
                        string[] IDSArray = ProDetailIDS.Split(Convert.ToChar(","));//详细产品IDS
                        if (!string.IsNullOrEmpty(IDSArray[i]) && IDSArray[i] != "0")
                        {
                            var model = bll_ProductsDetail.GetSingle_CNVP_ProductsDetailById(int.Parse(IDSArray[i].ToString()));
                            if (model != null)
                            {
                                _SalePrice = model.SalePrice;
                            }
                        }
                        else
                        {
                            if (model_Products != null)
                            {
                                _SalePrice = model_Products.TradePrice;
                            }
                        }

                    }
                    else
                    {
                        if (model_Products != null)
                        {
                            _SalePrice = model_Products.TradePrice;
                        }
                    }

                    decimal SalePrice_Decimal = 0;
                    if (this.CookieString != null)
                    {
                        SalePrice_Decimal = _SalePrice * DisCountPercent;
                    }
                    SalePriceTatol += SalePrice_Decimal * int.Parse(SaleNumArray[i]);
                }

                string html = "";
                html += "<h3>支付订单确认</h3>";
                html += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"  widtd=\"100%\">";
                html += "	 <tbody >";
                html += "		<tr >";
                html += "			<th >";
                html += "				商品总价格：</th>";
                html += "			<td  >";
                html += "			￥<span id=\"spanSalePriceTatol\">" + SalePriceTatol.ToString("f2") + "</span>";
                html += "			<input id=\"inputSalePriceTatol\" type=\"hidden\" name=\"inputSalePriceTatol\" value=\"" + SalePriceTatol.ToString("f2") + "\" /></td>";
                html += "		</tr>";
                html += "		<tr>";
                html += "			<th >";
                html += "			   配送费用：</th>";
                html += "			<td  >";
                html += "			￥<span id=\"spanDeliveryPrice\"></span>";
                html += "			<input id=\"inputDeliveryPrice\" type=\"hidden\" name=\"inputDeliveryPrice\" /></td>";
                html += "		</tr>";
                html += "		<tr >";
                html += "			<th>";
                html += "				订单总金额：</th>";
                html += "			<td  >";
                html += "			￥<span id=\"spanOrderTatol\">" + SalePriceTatol.ToString("f2") + "</span>";
                html += "			<input id=\"inputOrderTatol\" type=\"hidden\" name=\"inputOrderTatol\" value=\"" + SalePriceTatol.ToString("f2") + "\" /></td>";
                html += "		</tr>";
                html += "	</tbody>";
                html += "</table>";
                OrderCheck.InnerHtml = html;
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ProductIDS) && !string.IsNullOrEmpty(AmountS))
            {
                try
                {
                    if (Request.Form["ListProvince"] == "")
                    {
                        MessageUtils.ShowPre("请选择省区！");
                        return;
                    }
                    if (Request.Form["ListCity"] == null)
                    {
                        MessageUtils.ShowPre("请选择市区！");
                        return;
                    }
                    if (Request.Form["ListArea"] == null)
                    {
                        MessageUtils.ShowPre("请选择地区！");
                        return;
                    }
                    if (Request.Form["PayWay"] == null)
                    {
                        MessageUtils.ShowPre("请选择支付方式！");
                        return;
                    }
                    #region 主订单表
                    int UserID = CookieUserID;
                    string DeliveryWay = "";
                    decimal DeliveryPrice = 0;
                    string _DeliveryWayPrice = Request.Form["DeliveryWayPrice"];
                    if (!string.IsNullOrEmpty(_DeliveryWayPrice))
                    {
                        string[] arrWayPrice = _DeliveryWayPrice.Split(Convert.ToChar(":"));
                        if (arrWayPrice.Length > 0)
                        {
                            DeliveryWay = arrWayPrice[0];
                            DeliveryPrice = Convert.ToDecimal(arrWayPrice[1]);
                        }
                    }
                    int rows = new BLL.CNVP_Order().Add_CNVP_Order(new Model.CNVP_Order()
                    {
                        BuyerID = UserID,
                        AddTime = DateTime.Now,
                        PostTime = DateTime.Now,
                        OrderNum = DateTime.Now.ToString("yyyyMMddhhMMss"),
                        Province = Request.Form["ListProvince"],
                        City = Request.Form["ListCity"],
                        Area = Request.Form["ListArea"],
                        GetAddress = Request.Form["txtGetAddress"],
                        GetTrueName = Request.Form["txtTrueName"],
                        MobilePhone = Request.Form["txtMobilePhone"],
                        Phone = Request.Form["txtPhone"],
                        ZipCode = Request.Form["txtZipCode"],
                        DeliveryWay = DeliveryWay,
                        DeliveryPrice = DeliveryPrice,
                        PayWay = Request.Form["PayWay"],
                        ProductsTotalAmount = decimal.Parse(Request.Form["inputSalePriceTatol"]),
                        OrderTotalAmount = decimal.Parse(Request.Form["inputOrderTatol"]),
                        IsNeedBill = Request.Form["GetInvoice"] == "1" ? Convert.ToByte(1) : Convert.ToByte(0),
                        OrderState = Convert.ToInt16(Request.Form["PayWay"]) == (byte)103 ? (byte)1 : (byte)0, //货到付款‘103’还是在线支付
                        Text=txtText.Text.Trim()
                    });
                    #endregion

                    #region 添加订单产品详细表
                    string LengthName_Str = "";
                    string ImgProLoad_Str = "";
                    int ProductClassID_Int = 0;
                    string ProDetailNo_Str = "";
                    decimal SalePrice_decimal = 0;
                    int SaleNum_Int = 0;
                    decimal TotalPrice_decimal = 0;
                    int productID_Int = 0;
                    int proDetailID_Int = 0;

                    string _LengthName = Request.Form["LengthName"];
                    string _ImgProLoad = Request.Form["ImgProLoad"];
                    string _ProductClassID = Request.Form["ProductClassID"];
                    string _ProDetailNo = Request.Form["ProDetailNo"];
                    string _SalePrice = Request.Form["SalePrice"];
                    string _SaleNum = Request.Form["Num"];

                    string[] PIDSArray = ProductIDS.Split(Convert.ToChar(","));
                    for (int i = 0; i < PIDSArray.Length; i++)
                    {
                        string[] productIDValue = ProductIDS.Split(Convert.ToChar(","));
                        productID_Int = Convert.ToInt32(productIDValue[i]);
                        string[] LengthNameValue = _LengthName.Split(Convert.ToChar(","));
                        LengthName_Str = LengthNameValue[i];
                        string[] ImgProLoadValue = _ImgProLoad.Split(Convert.ToChar(","));
                        ImgProLoad_Str = ImgProLoadValue[i];
                        string[] ProductClassIDValue = _ProductClassID.Split(Convert.ToChar(","));
                        ProductClassID_Int = Convert.ToInt32(ProductClassIDValue[i]);
                        string[] ProDetailNoValue = _ProDetailNo.Split(Convert.ToChar(","));
                        ProDetailNo_Str = ProDetailNoValue[i];
                        string[] SalePriceValue = _SalePrice.Split(Convert.ToChar(","));
                        SalePrice_decimal = Convert.ToDecimal(SalePriceValue[i]);
                        string[] SaleNumArray = _SaleNum.Split(Convert.ToChar(","));//销售数量
                        try
                        {
                            SaleNum_Int = Convert.ToInt32(SaleNumArray[i]);
                        }
                        catch { SaleNum_Int = 0; }
                        TotalPrice_decimal = SalePrice_decimal * SaleNum_Int;//计算总额

                        if (!string.IsNullOrEmpty(ProDetailIDS))//详细产品IDS
                        {
                            string[] IDSArray = ProDetailIDS.Split(Convert.ToChar(","));//详细产品IDS
                            proDetailID_Int = Convert.ToInt32(IDSArray[i]);

                        }

                        int rows_OrderProducts = new BLL.CNVP_OrderProducts().Add_CNVP_OrderProducts(new Model.CNVP_OrderProducts()
                        {
                            ProductName = LengthName_Str,
                            ImgProLoad = ImgProLoad_Str,
                            ProductClassID = ProductClassID_Int,
                            ProductID = productID_Int,
                            ProductsDetailID = proDetailID_Int,
                            ProductNo = ProDetailNo_Str,
                            Price = SalePrice_decimal,
                            Num = SaleNum_Int,
                            TotalPrice = TotalPrice_decimal,
                            OrderID = rows
                        });
                    }
                    #endregion

                    #region 购物必填信息
                    try
                    {
                        int ProID_Int = 0;
                        int ProDetailID_Int = 0;
                        string _ProID_ProDetailIDS = Request.Form["ProID_ProDetailIDS"];
                        if (!string.IsNullOrEmpty(_ProID_ProDetailIDS))
                        {
                            string[] ProID_ProDetailIDSArray = _ProID_ProDetailIDS.Split(Convert.ToChar(","));
                            for (int k = 0; k < ProID_ProDetailIDSArray.Length; k++)
                            {
                                string[] IDSArray = ProID_ProDetailIDSArray[k].Split(Convert.ToChar("_"));
                                ProID_Int = Convert.ToInt32(IDSArray[0]);//主产品ID
                                ProDetailID_Int = int.Parse(IDSArray[1]);//产品详细ID

                                int ProTypeRefShopMustInfoID_Int = Convert.ToInt32(Request.Form["CKRefShopMustInfo" + ProID_ProDetailIDSArray[k] + ""]);
                                string _MustInfoValue = Request.Form["MustInfoValue" + ProID_ProDetailIDSArray[k] + ""];
                                if (!string.IsNullOrEmpty(_MustInfoValue))
                                {
                                    int rows_OrderProShopMustInfo = new BLL.CNVP_OrderProShopMustInfo().Add_CNVP_OrderProShopMustInfo(new Model.CNVP_OrderProShopMustInfo()
                                    {
                                        ProductID=ProID_Int,
                                        ProDetailsID = ProDetailID_Int,
                                        OrderID = rows,
                                        ProTypeRefShopMustInfoID = ProTypeRefShopMustInfoID_Int,
                                        StandardValue = _MustInfoValue
                                    });
                                }
                            }
                        }
                    }
                    catch
                    { }
                    #endregion

                    #region 编辑，保存购物地址
                    if (Request.Form["EditGetAddressID"] != null)
                    {
                        Model.CNVP_GetAddress model_GetAddress = new Model.CNVP_GetAddress();
                        model_GetAddress.ID = int.Parse(Request.Form["EditGetAddressID"]);
                        model_GetAddress.Province = Request.Form["ListProvince"];
                        model_GetAddress.City = Request.Form["ListCity"];
                        model_GetAddress.Area = Request.Form["ListArea"];
                        model_GetAddress.GetAddress = Request.Form["txtGetAddress"];
                        model_GetAddress.TrueName = Request.Form["txtTrueName"];
                        model_GetAddress.MobilePhone = Request.Form["txtMobilePhone"];
                        model_GetAddress.Phone = Request.Form["txtPhone"];
                        model_GetAddress.ZipCode = Request.Form["txtZipCode"];
                        model_GetAddress.UsersID = UserID;
                        new BLL.CNVP_GetAddress().Update_CNVP_GetAddressWithout(model_GetAddress);
                    }
                    if (Request.Form["cbIsSave"] == "on")
                    {
                        int rows_GetAddress = new BLL.CNVP_GetAddress().Add_CNVP_GetAddress(new Model.CNVP_GetAddress()
                        {
                            Province = Request.Form["ListProvince"],
                            City = Request.Form["ListCity"],
                            Area = Request.Form["ListArea"],
                            GetAddress = Request.Form["txtGetAddress"],
                            TrueName = Request.Form["txtTrueName"],
                            MobilePhone = Request.Form["txtMobilePhone"],
                            Phone = Request.Form["txtPhone"],
                            ZipCode = Request.Form["txtZipCode"],
                            IsTolerant = 0,
                            UsersID = UserID
                        });
                    }

                    #endregion

                    BLL.BLLShoppingCar cart = new BLL.BLLShoppingCar();
                    cart.Delete(ProductIDS, ProDetailIDS, AmountS);//清除Cookie

                    BasePage.MessageBox("订单生成成功！", "OrderShow.aspx?id=" + rows);//先转到订单详细页面
                }
                catch
                {
                    MessageUtils.ShowPre("悲剧，出错了！");
                }
            }
        }
    }
}